package com.zhangyj.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhangyj.BoastException;
import com.zhangyj.LocalMessage;
import com.zhangyj.data.tables.BProgram;
import com.zhangyj.db.DBUtil;
import com.zhangyj.entitis.SystemParam;
import com.zhangyj.impl.UserContext;

public class FunctionAction extends HttpServlet {
	private static final long serialVersionUID = -7217689131311828796L;

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getRequestURI();
		String pid = path.substring(path.lastIndexOf('/') + 1, path.lastIndexOf('.'));
		try {
			if (pid == null || pid.length() == 0) {
				throw new BoastException(LocalMessage.get("error.noprogram"));
			}
			if (UserContext.isAnonymity(request)) {
				throw new com.zhangyj.security.AuthenticationException(LocalMessage.get("error.nologin"));
			}
			int i_pid = Integer.parseInt(pid);
			SystemParam systemParam = SystemParam.getSystemParam(request);
			systemParam.setFunctionId(i_pid);
			//UserContext usercontext = UserContext.getUserContext(request);
			
			BProgram p = DBUtil.queryOne("select * from b_program where programid=?", BProgram.class, i_pid);// ss.getMapper(BProgramMapper.class).selectByPrimaryKey(i_pid);
			String resource = p.getProgramResource();
			/*
			if (resource == null || resource.trim().length() == 0) {
				throw new BoastException(LocalMessage.get("nopremission"));
			}
			*/
			request.getRequestDispatcher(resource).forward(request, response);
		} catch (Exception e) {
			request.setAttribute("returnPath", path + "?" + request.getQueryString());
			throw new ServletException(e);
		}finally{
		}
	}
}
